import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wan_android_flutter/common/base/base_page.dart';
import 'package:wan_android_flutter/common/resources/text_styles.dart';

import '../../../../../common/network/model/knowledges_model.dart';
import '../../../../../common/network/model/single_knowledges_model.dart';
import '../../../../../common/resources/spacing.dart';
import '../../../../../common/routes/route_manager.dart';
import '../../../../../common/widget/material_layout.dart';
import '../../../../../res/assets_res.dart';
import '../../../../common_web/common_web_state.dart';
import 'single_knowledge_logic.dart';

class SingleKnowledgePage extends BasePage<SingleKnowledgeLogic> {
  SingleKnowledgePage(this.model, {super.key});

  final KnowledgeModel model;

  @override
  String? get tag => model.name ?? "";

  @override
  bool showAppBar() {
    return false;
  }

  @override
  Widget buildContent(BuildContext context) {
    return Container(
      color: colorData.bgWhite,
      child: EasyRefresh(
          controller: logic.controller,
          onRefresh: () {
            logic.state.pageIndex = 0;
            logic.loadSingleKnowledges();
          },
          onLoad: () {
            logic.state.pageIndex += 1;
            logic.loadSingleKnowledges();
          },
          child: Obx(() {
            return ListView.separated(
              itemBuilder: (context, index) {
                return _buildItem(logic.state.singleKnowledges[index], index);
              },
              itemCount: logic.state.singleKnowledges.length,
              separatorBuilder: (context, index) => Container(
                height: 16.0,
                color: colorData.bgDivider,
              ),
            );
          })),
    );
  }

  Widget _buildItem(SingleKnowledgeModel singleKnowledge, int index) {
    return MaterialLayout(
      backgroundColor: colorData.bgWhite,
      onPressed: () {
        CommonBrowserParam commonBrowserParam = CommonBrowserParam(
          title: singleKnowledge.title ?? "",
          url: singleKnowledge.link ?? "",
        );
        Get.toNamed(RouteManager.commonWebPage, arguments: commonBrowserParam);
      },
      padding: const EdgeInsets.all(16),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Image.asset(
                AssetsRes.IC_LAUNCHER_ROUND,
                width: 25,
                height: 25,
              ),
              Text(
                singleKnowledge.niceDate ?? "",
                style: colorData.textGray.s14.regular,
              )
            ],
          ),
          Spacing.vSpacing8,
          Text(
            singleKnowledge.title ?? "",
            style: colorData.textBlack.s20.regular,
          ),
          Spacing.vSpacing8,
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Image.asset(
                AssetsRes.HEART,
                width: 25,
                height: 25,
              )
            ],
          ),
        ],
      ),
    );
  }
}
